package com.whiteape.book.ui.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.whiteape.book.R;
import com.whiteape.book.contract.BookCategoryListContract;
import com.whiteape.book.model.type.BookSortListType;
import com.whiteape.book.model.zssq.SortBookBean;
import com.whiteape.book.presenter.BookCategoryListPresenter;
import com.whiteape.book.ui.activity.SearchActivity;
import com.whiteape.book.ui.adapter.BookSortListAdapter;
import com.whiteape.book.ui.base.BaseMVPFragment;
import com.whiteape.book.ui.base.adapter.WholeAdapter;
import com.whiteape.book.widget.RefreshLayout;
import com.whiteape.book.widget.itemdecoration.DividerItemDecoration;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class BookSortListFragment extends BaseMVPFragment<BookCategoryListContract.Presenter> implements BookCategoryListContract.View {

    private static final String EXTRA_GENDER = "extra_gender";
    private static final String EXTRA_TYPE = "extra_type";
    private static final String EXTRA_MAJOR = "extra_major";

    /********************/
    @BindView(R.id.refresh_layout)
    RefreshLayout mRefreshLayout;
    @BindView(R.id.refresh_rv_content)
    RecyclerView mRvContent;
    /*************************************/
    BookSortListAdapter mBookSortListAdapter;
    /************************************/
    /************************************/
    private String mGender;
    private String mMajor;
    private BookSortListType mType;
    private String mMinor = "";
    private int mStart = 0;
    private int mLimit = 20;
    private Unbinder unbinder;

    public static Fragment newInstance(String gender, String major, BookSortListType type) {
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_GENDER, gender);
        bundle.putString(EXTRA_MAJOR, major);
        bundle.putSerializable(EXTRA_TYPE, type);
        Fragment fragment = new BookSortListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void showError() {

    }

    protected void initListener() {
        mBookSortListAdapter.setOnItemClickListener(
                (view, pos) -> {
                    String bookTitle = mBookSortListAdapter.getItem(pos).getTitle();
//                    BookDetailActivity.startActivity(getContext(),bookId);
                    //跳转到搜索页面

                    Intent intent = new Intent(getContext(), SearchActivity.class);
                    intent.putExtra("searchKey", bookTitle);
                    Objects.requireNonNull(getActivity()).startActivity(intent);


                }
        );

        mBookSortListAdapter.setOnLoadMoreListener(
                () -> mPresenter.loadSortBook(mGender, mType, mMajor, mMinor, mStart, mLimit)
        );

        //子类的切换

        mRefreshLayout.showLoading();
//        mPresenter.refreshSortBook(mGender, mType, mMajor, mMinor, mStart, mLimit);

//        Disposable disposable = RxBus.get()
//                .toObservable(BookSubSortEvent.class)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        (event) -> {
//                            mMinor = event.bookSubSort;
//                            mRefreshLayout.showLoading();
//                            mStart = 0;
//                            mPresenter.refreshSortBook(mGender,mType,mMajor,mMinor,mStart,mLimit);
//                        }
//                );
//        addDisposable(disposable);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setUpAdapter();
    }

    private void setUpAdapter() {
        mBookSortListAdapter = new BookSortListAdapter(getContext(), new WholeAdapter.Options());

        mRvContent.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvContent.addItemDecoration(new DividerItemDecoration(getContext()));
        mRvContent.setAdapter(mBookSortListAdapter);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (savedInstanceState != null) {
            mGender = savedInstanceState.getString(EXTRA_GENDER);
            mMajor = savedInstanceState.getString(EXTRA_MAJOR);
            mType = (BookSortListType) savedInstanceState.getSerializable(EXTRA_TYPE);
        } else {
            mGender = getArguments().getString(EXTRA_GENDER);
            mMajor = getArguments().getString(EXTRA_MAJOR);
            mType = (BookSortListType) getArguments().getSerializable(EXTRA_TYPE);
        }
    }

    @Override
    protected void startLogic() {
        super.startLogic();
        initListener();
        mRefreshLayout.showLoading();
        mPresenter.refreshSortBook(mGender, mType, mMajor, mMinor, mStart, mLimit);
    }

    @Override
    protected int getContentId() {
        return R.layout.fragment_refresh_list;
    }

    @Override
    protected BookCategoryListContract.Presenter bindPresenter() {
        return new BookCategoryListPresenter();
    }

    @Override
    public void finishRefresh(List<SortBookBean> beans) {
        if (beans.isEmpty()) {
            mRefreshLayout.showEmpty();
            return;
        }
        mBookSortListAdapter.refreshItems(beans);
        mStart = beans.size();
    }

    @Override
    public void finishLoad(List<SortBookBean> beans) {

        mBookSortListAdapter.addItems(beans);
        mStart += beans.size();
    }

    @Override
    public void showLoadError() {

        mBookSortListAdapter.showLoadError();
    }

    @Override
    public void complete() {
        mRefreshLayout.showFinish();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
